package 线程的状态;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-11-22
 * Time: 16:37
 */
public class ApplyTestDemo {

    /**
     * 模拟两个人协作一起烧壶泡茶过程
     * 假如 : 小李和老李进行模拟
     * 老李进行 洗水壶(1s)和烧开水(5s)
     * 小李进行 洗茶壶(1s),洗茶杯(2s),拿茶叶(1s)
     * 然后小李做完这些工作之后,然后等待老李做完,然后小李进行泡茶
     *
     * 使用多线程的方式可以最大化的利用时间,节省时间,提高效率
     */
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance(); // get current instance of the calendar
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        System.out.println(formatter.format(calendar.getTime()));
        Thread t1 = new Thread(()->{
             //1s钟洗水壶
             System.out.println(formatter.format(calendar.getTime()) + "洗水壶!!!");
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             //5s钟烧开水
             System.out.println(formatter.format(calendar.getTime()) + "烧开水!!!");
             try {
                 Thread.sleep(5000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         },"老李");
         Thread t2 = new Thread(()->{
             //1s中洗茶壶
             System.out.println(formatter.format(calendar.getTime()) + "洗茶壶!!!");
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             //2s钟洗茶杯
             System.out.println(formatter.format(calendar.getTime()) + "洗茶杯!!!");
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             //1s钟拿茶叶
             System.out.println(formatter.format(calendar.getTime()) + "拿茶叶!!!");
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             //小李等待老李做完他做的工作,小李开始泡茶
             try {
                 t1.join();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             System.out.println(formatter.format(calendar.getTime()) + "泡茶!!!");
         },"小李");
         t1.start();
         t2.start();
    }
}
